<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->


<dom-module id="shop-my-score">
    <template>
        <style include="shop-common-styles">
            .score{
                display: flex;
                align-items: center;
                justify-content: center;
                flex-direction: column;
            }
            .score p{
                border-radius: 50%;
                width: 120px;
                height: 120px;
                background-color: green;
                color: #fff;
                display: flex;
                align-items: center;
                justify-content: center;
                padding: 0px;
                margin: 20px;
                font-size: 24px;
            }
            .plugins{
                display: flex;
                flex-wrap: wrap;
            }
            .plugins div{
                display: flex;
                justify-content: center;
                align-items: center;
                flex-direction: column;
                width: 20%;
                padding: 5px;
                box-sizing: border-box;
            }
            .plugins img{
                width: 100%;
            }
            h3{
                background-color: #eee;
                text-align: center;
                font-size: 14px;
                line-height: 30px;
            }
        </style>
        <plugin-xhr id="xhr_plugin_list_by_category_point" url="[[app.apiHost]]/plugin.list.by.category.point" response="{{plugins}}" response-property="result"></plugin-xhr>
        <plugin-xhr id="xhr_account_get_current" url="[[app.apiHost]]/account.get.current" response="{{account}}" response-property="result"></plugin-xhr>
        <plugin-xhr id="xhr_open_page" url="[[app.apiHost]]/wx.app.call?name=openPage"></plugin-xhr>
        <div class="score">
            <p>[[account.score]]</p>
            <span>我的积分</span>
        </div>
        <h3 class="title" hidden="[[pluginsIsEmpty]]">
            立即使用积分
        </h3>
        <div class="plugins">
        <template is="dom-repeat" items="[[plugins]]" as="plugin">
            <div>
                <img on-click="_openPlugin" src="[[plugin.config.icon]]"/>
                <span>[[plugin.display_name]]</span>
            </div>
        </template>
        </div>
    </template>

    <script>
        class ShopMyScore extends PluginElement {

            static get is() {
                return 'shop-my-score';
            }

            static get properties() {
                return {
                    visible: {
                        type: Boolean,
                        observer: '_visibleChanged'
                    },
                    pluginsIsEmpty:{
                        type:Boolean,
                        value:true
                    }
                }
            }
            ready(){
                super.ready();
                let dom = this;
                this.$.xhr_plugin_list_by_category_point.param("plugin_category","sale").param("plugin_point","%my-score%").param("app_id",app.appId).callback(function(res){
                    dom.pluginsIsEmpty = res.length==0;
                }).get();
            }
            _visibleChanged(visible) {
                if(visible){
                    this.$.xhr_account_get_current.get();
                }
            }
            _openPlugin(e){
                this.$.xhr_open_page.setBody({page:app.httpHost+"/"+e.model.plugin.id+".pl/"+e.model.plugin.config.point["shop-home-float"]+"?app_id="+app.appId}).post();
            }
        }

        customElements.define(ShopMyScore.is, ShopMyScore);

    </script>

</dom-module>
